package com.young.common.expression.dns;

import org.apache.commons.lang3.StringUtils;

import java.net.InetAddress;
import java.util.*;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:46 PM
 */
public class PropertyResolver implements NamingResolver {
    private final NamingResolver DEFAULT;
    private final Map<String, String> dc;
    private final Map<String, String> sc;

    public PropertyResolver() {
        this.DEFAULT = DefaultResolver.INSTANCE;
        this.dc = new LinkedHashMap();
        this.sc = new LinkedHashMap();
    }

    public InetAddress[] resolve(String target) {
        String value = CachedMatcher.find(this.dc, target);
        if (value == null) {
            value = CachedMatcher.find(this.sc, target.toUpperCase());
        }

        return value == null ? this.DEFAULT.resolve(target) : this.DEFAULT.resolve(value);
    }

    public InetAddress[] domainResolve(String domain) {
        String value = CachedMatcher.find(this.dc, domain);
        return value != null ? this.DEFAULT.resolve(value) : this.DEFAULT.resolve(domain);
    }

    public InetAddress[] serviceResolve(String service) {
        String value = CachedMatcher.find(this.sc, service.toUpperCase());
        return value != null ? this.DEFAULT.resolve(value) : this.DEFAULT.resolve(service.toUpperCase());
    }

    public String findService(String service) {
        return CachedMatcher.find(this.sc, service.toUpperCase());
    }

    private synchronized void put(String pattern, String target) {
        if (pattern.startsWith("domain.")) {
            this.addDomainResolver(StringUtils.substringAfter(pattern, "domain."), target);
        } else if (pattern.startsWith("service.")) {
            this.addServiceResolver(StringUtils.substringAfter(pattern, "service."), target);
        }

    }

    public synchronized void load(Properties properties) {
        if (properties != null && !properties.isEmpty()) {
            Set<String> names = properties.stringPropertyNames();
            Iterator var3 = names.iterator();

            while(var3.hasNext()) {
                String name = (String)var3.next();
                String value = properties.getProperty(name);
                if (StringUtils.isNotBlank(value)) {
                    this.put(name.trim(), value.trim());
                }
            }

        }
    }

    public synchronized void addDomainResolvers(Map<String, String> domainResolvers) {
        if (domainResolvers != null) {
            domainResolvers.forEach((key, value) -> {
                if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
                    this.dc.put(key.trim(), value.trim());
                }

            });
        }

    }

    public Map<String, String> copyServiceResolvers() {
        return new LinkedHashMap(this.sc);
    }

    public Map<String, String> copyDomainResolvers() {
        return new LinkedHashMap(this.dc);
    }

    public Map<String, String> copyServiceGroupResolvers() {
        return null;
    }

    public synchronized void addServiceResolvers(Map<String, String> serviceResolvers) {
        if (serviceResolvers != null) {
            serviceResolvers.forEach((key, value) -> {
                if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
                    this.sc.put(key.trim(), value.trim());
                }

            });
        }

    }

    public synchronized void addDomainResolver(String domain, String target) {
        this.dc.put(domain, target);
    }

    public synchronized void addServiceResolver(String service, String target) {
        this.sc.put(service, target);
    }
}
